const workService = require('../services/taskService')

const getTaskByUserId = async (req, res) => {
  try {
    const result = await workService.getTaskByUserId(req.query.userId)
    res.json({
      status: 0,
      data: result
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

const getTaskModel = async (req, res) => {
  try {
    const result = await workService.getTaskModel()
    res.json({
      status: 0,
      data: result
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

const getNoteByUserId = async (req, res) => {
  try {
    const result = await workService.getNoteByUserId(req.query.userId)
    res.json({
      status: 0,
      data: result
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

const addNewTask = async (req, res) => {
  try {
    await workService.addNewTask(req.body)
    res.json({
      status: 0,
      data: '保存成功'
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

const addNewNote = async (req, res) => {
  try {
    await workService.addNewNote(req.body)
    res.json({
      status: 0,
      data: '保存成功'
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

const addNewTaskModel = async (req, res) => {
  try {
    await workService.addNewTaskModel(req.body)
    res.json({
      status: 0,
      data: '保存成功'
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

const deleteTaskById = async (req, res) => {
  try {
    await workService.deleteTaskById(req.query.id)
    res.json({
      status: 0,
      data: '删除成功'
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

const delTaskModel = async (req, res) => {
  try {
    await workService.delTaskModel(req.query.id)
    res.json({
      status: 0,
      data: '删除成功'
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

const handOutTask = async (req, res) => {
  try {
    await workService.handOutTask(req.body)
    res.json({
      status: 0,
      data: '任务分发成功'
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

const updateTask = async (req, res) => {
  try {
    await workService.updateTask(req.query)
    res.json({
      status: 0,
      data: '保存成功'
    })
  } catch (err) {
    res.json({
      status: 1,
      msg: err.message
    })
  }
}

module.exports = {
  getTaskByUserId,
  getTaskModel,
  handOutTask,
  getNoteByUserId,
  updateTask,
  deleteTaskById,
  addNewNote,
  addNewTask,
  addNewTaskModel,
  delTaskModel
}
